package nml.webmail;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public abstract class WebMail {
	WebDriver dr = null;
	String jq = null;

	public WebMail() {
		System.setProperty("java.io.tmpdir", "z:\\");
		System.setProperty("webdriver.firefox.bin", "z:\\Firefox\\firefox.exe");

		FirefoxProfile fp = new FirefoxProfile(new File("z:\\firefoxprofile2"));

		dr = new FirefoxDriver(fp);
		
		try {
			jq = FileUtils.readFileToString(new File("jquery.min.js"));
		} catch (IOException e) {
			e.printStackTrace();
		}
 	}

	public void loadJQuery() {
		executeScript(jq);
	}

	public void sleep(long s) {
		try {
			Thread.sleep(s);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public WebElement waitWebElement(final By b) {
		return waitWebElement(b, 10);
	}

	public WebElement waitWebElement(final By b, int sec) {
		return new WebDriverWait(dr, sec).until(new ExpectedCondition<WebElement>() {
			@Override
			public WebElement apply(WebDriver dr) {
				return dr.findElement(b);
			}
		});
	}

	public Object executeScript(String js) {
		return ((JavascriptExecutor) dr).executeScript(js);
	}

	public abstract void send(String uid, String password, String to, String subject, String content) throws Exception;

	public void success(String to) {

	}

	public String loadJS() throws IOException {
		return FileUtils.readFileToString(new File(this.getClass().getSimpleName()+".js"));
	}

}
